package cmd

import (
	_ "gitee.com/keepalived_0807/Vblog/apps"
	initCmd "gitee.com/keepalived_0807/Vblog/cmd/init"
	"gitee.com/keepalived_0807/Vblog/cmd/start"
	"gitee.com/keepalived_0807/Vblog/conf"
	"gitee.com/keepalived_0807/Vblog/ioc"
	"github.com/spf13/cobra"
)

func Execute() error {
	// 初始化cli
	cobra.OnInitialize(func() {
		cobra.CheckErr(conf.LoadConfigFromYaml(configPath))
		cobra.CheckErr(ioc.Controller.Init())
		cobra.CheckErr(ioc.Api.Init())
	})
	return RootCmd.Execute()
}

var (
	configPath string
	RootCmd    = &cobra.Command{
		Use:   "vblog",
		Short: "vblog service",
		Long:  ``,
		Run: func(cmd *cobra.Command, args []string) {
			if len(args) > 0 {
				if args[0] == "version" {
					cmd.Println("version: 0.0.1")
					return
				}
			} else {
				cmd.Help()
			}
		},
	}
)

func init() {
	RootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "etc/application.yaml", "the service config from file")
	RootCmd.AddCommand(initCmd.Cmd)
	RootCmd.AddCommand(start.Cmd)
}
